<template>
	<view class="container">
		<!-- #ifdef H5 -->
		<uni-header></uni-header>
		<!-- #endif -->

		<view class="bid-box com-box">
			<view class="navbar">
				<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex === index}"
				 @click="tabClick(index)">
					{{item.text}}
				</view>
			</view>

			<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
				<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
					<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
						<!-- 空白页 -->
						<empty v-if="tabItem.loaded === true && tabItem.bidList.length === 0"></empty>

						<!-- 投标列表 -->
						<view class="bid-list">
							<block v-for="(item,index) in tabItem.bidList" :key="index">
								<view class="bid-item">
									<text class="title">{{item.tbPublishingNeeds.goodsName}}</text>
									<text class="num">数量：{{item.tbPublishingNeeds.num}}吨</text>
									<text class="size">规格：{{item.tbPublishingNeeds.specifications}}</text>
									<view class="text-wrapper">
										<view class="shop clamp">
											<image :src="item.tbPublishingNeeds.imgUrl" mode="aspectFill"></image>
											{{item.tbPublishingNeeds.sellerName}}
										</view>
										<view class="time">
											<text>截止时间：{{$util.sliceTime(item.tbPublishingNeeds.effectiveTime)}}</text>
											<text>送达时间：{{$util.sliceTime(item.tbPublishingNeeds.sendTime)}}</text>
										</view>
									</view>
									<view class="action-box" v-if="item.status == 0">

										<button type="primary" :disabled="submiting" class="action-btn del" @click="delBid(item)">删除</button>
									</view>
									<view class="action-box" v-if="item.status == 2">
										<button type="primary" :disabled="submiting" class="action-btn" @click="showBid(item)">联系对方</button>
									</view>
								</view>
							</block>
						</view>
						<uni-load-more :status="tabItem.loadingType"></uni-load-more>

					</scroll-view>
				</swiper-item>
			</swiper>
		</view>

		<!-- #ifdef H5 -->
		<uni-footer></uni-footer>
		<!-- #endif -->
	</view>
</template>

<script>
	import uniModalNext from '@/components/uni-modal-next.vue'
	import uniLoadMore from '@/components/uni-load-more.vue'
	import empty from "@/components/empty.vue"
	import uniHeader from '@/components/h5/uni-header.vue'
	import uniFooter from '@/components/h5/uni-footer.vue'
	export default {
		components: {
			uniLoadMore,
			empty,
			uniModalNext,
			uniHeader,
			uniFooter
		},
		data() {
			return {
				submiting: false, //防抖
				inputRefundReason: '',
				refundOrderItem: '',
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '已投标',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						bidList: []
				},
					{
						state: 2,
						text: '已中标',
						loadingType: 'more',
						pageNum: 2,
						pageSize: 10,
						bidList: []
				}
			],
			}
		},
		onLoad(option) {
			this.tabCurrentIndex = 0
			for (let i = 0; i < this.navList.length; i++) {
				if (this.navList[i].state === parseInt(option.state)) {
					this.tabCurrentIndex = i
				}
			}
			this.loadData()
		},
		methods: {
			//获取招标列表
			loadData(source) {
				//这里是将招标挂载到tab列表下
				let index = this.tabCurrentIndex
				let navItem = this.navList[index]
				let state = navItem.state
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return
				}
				navItem.loadingType = 'loading'
				this.$api.request('/publishingNeeds/winningLogPage', {
					pageNum: navItem.pageNum,
					pageSize: navItem.pageSize,
					status: navItem.state
				}, {
					method: 'GET'
				}).then(res => {
					navItem.loadingType = navItem.pageNum < res.data.total ? 'more' : 'noMore'
					navItem.pageNum = navItem.pageNum + 1
					// navItem.loadingType = 'noMore'
					res.data.pageList.forEach(item => {
						item.status = navItem.state
						navItem.bidList.push(item)
					})
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true)
				})
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current
				this.loadData('tabChange')
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index
			},
			//删除招标
			delBid(item) {
				uni.showModal({
					title: '删除?',
					content: '您确定要删除此招标吗?',
					success: (e) => {
						if (e.confirm) {
							this.submiting = true
							let idx = this.tabCurrentIndex
							let navItem = this.navList[idx]
							this.$api.request('/publishingNeeds/deletePublishingNeed', {
								id: item.id
							}, {
								method: 'GET',
								failback: failres => {
									this.submiting = false
									this.$api.msg(failres.message)
								}
							}).then(res => {
								this.$api.msg(res.message)
								this.submiting = false
								navItem.bidList.splice(index, 1)
							})
						}
					}
				})
			},
			showBid(item) {
				uni.showModal({
					title: '联系电话',
					content: item.phone,
					success: (e) => {}
				})
			}
		}
	}
</script>

<style lang="scss">
	page,
	.bid-box,
	.container {
		height: 100%;
	}

	.container {
		padding-top: 80upx;
	}

	.swiper-box,
	.list-scroll-content {
		height: 100%;
	}

	.uni-swiper-item {
		height: auto;
	}

	.bid-list {
		border-top: 20upx solid $base-color-bg;
	}

	/* #ifdef H5 */
	@media screen and (min-width: 750px) {

		page,
		.bid-box,
		.container {
			height: auto;
		}

		.container {
			padding-top: 0;
		}

		.bid-box {
			margin: 20px auto;
		}

		.navbar {
			background-color: #e0edff;
		}

		.swiper-box {
			height: 100vh;
			background-color: #fff;
		}

		.list-scroll-content {
			height: 100vh;
		}

		.uni-swiper-item {
			height: auto;
		}

		.bid-list {
			border-top: 0;
		}
	}

	/* #endif */
</style>
